package tools.Project3.Group3.s12cs342;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import activities.project3.group3.s12cs342.AuthenticateActivity;
import activities.project3.group3.s12cs342.QuizMasterActivity;
import activities.project3.group3.s12cs342.R;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AsyncDownload extends AsyncTask <Void, Integer, ArrayList> {
	String url;
	@SuppressWarnings("rawtypes") ArrayList prams;
	Context context;
	EditText un;
	TextView error;
	private HttpPost httppost;
	private StringBuffer buffer;
	private HttpResponse response;
	private HttpClient httpclient;
	private InputStream inputStream;
	private byte[] data;
	Context c;
	
	@SuppressWarnings("rawtypes")
	public AsyncDownload(Context context, String url, Context c){
		this.context = context;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	protected ArrayList doInBackground(Void... params) {
		ArrayList results = new ArrayList();
		URL url = null;

		//System.out.println("Test1");
		try {
			try {
				// NEEDED VARIABLES URL READER LENGTH
				url = new URL("http://cs346.cs.uwosh.edu/students/lipinb34/readBlob.php");
				//System.out.println("Test2");
				// CONNECT SET AND REQUEST SET
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();

				connection.setUseCaches(false);
				connection.setDoOutput(true);

				BufferedReader br = new BufferedReader(new InputStreamReader(
						connection.getInputStream()));

				String temp;
				try {// check it against read to see if null
					temp = br.readLine();
					while (temp != null) {
						temp = temp.replaceAll(Pattern.quote("&quot"), "\"");
						temp = temp.replaceAll(Pattern.quote("&lt"), "<");
						temp = temp.replaceAll(Pattern.quote("&gt"), ">");
						temp = temp.replaceAll(Pattern.quote(";"), "");
						
						System.out.println(temp);
						results.add(temp);
						temp = br.readLine();
					}
					//System.out.println("Test3");
				} catch (IOException e) {
					/* End Loop */
				} finally {
					// br.close();
				}

			} catch (MalformedURLException e) {
				System.out.println("URL incorrectly formed");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {

			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}




		String eol = System.getProperty("line.separator");
		BufferedWriter writer = null;
		try {

			writer = new BufferedWriter(new OutputStreamWriter(context.openFileOutput(
					"myfile", Context.MODE_PRIVATE)));

			writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Collection>");
			for(int i = 0; i < results.size(); i++){
				writer.write((String)results.get(i));
			}
			writer.write("</Collection>");
			
			writer.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		

	}

	protected void onPostExecute(String res){
		
	}

}

